package com.xy.config;


import com.xy.realm.ShiroRealm;
import com.xy.realm.ShiroService;

import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;




@Configuration

public class ShiroConfig {
    @Autowired
    ShiroService shiroService;

    /*@Bean
    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }*/

    @Bean("realm")
    public Realm getRealm(){
        return new ShiroRealm();
    }

    @Bean("securityManager")
    public DefaultWebSecurityManager getSecurityManager(@Qualifier("realm") Realm realm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }
    @Bean
    public ShiroFilterFactoryBean getFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);



        shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroService.permsMap());
        shiroFilterFactoryBean.setLoginUrl("/login/err");
        shiroFilterFactoryBean.setUnauthorizedUrl("/auth/err");
        return shiroFilterFactoryBean;
    }
}
